<template>
  <div :class="{'mobile-help-dialog': showMobilePage}" class="help-dialog">
    <div :class="{'mobile-title': showMobilePage}" class="title">
      <div class="title-text">
        <img :src="showMobilePage?mobileHelpTitle:helpTitle" alt="title">
      </div>
      <div v-if="!showMobilePage" class="dialog-title-mask">
        <img :src="dialogTitleMask" alt="dialogTitleMask">
      </div>
      <div :class="{'mobile-title-btn':showMobilePage}" class="title-btn">
        <div class="to-help" @click="toHelpList"><span>{{ `${showMobilePage?'前往':'前往帮助中心'}` }}</span> >></div>
        <el-button v-if="(hasRouteAuth('crm:help:add') || isAdmin) && !showMobilePage" type="info" size="small" class="add-article" @click="addArticle">
          添加文章
          <i class="el-icon-plus" style="font-size: 16px;" />
        </el-button>
      </div>
    </div>
    <div class="help-search">
      <el-input
        v-if="showMobilePage"
        v-model="keyWord"
        type="text"
        size="small"
        placeholder="搜索你有疑问的功能"
        class="form-input"
        style="font-size:14px;"
        clearable
        @keyup.enter.native="toSearch"
      >
        <svg-icon slot="append" icon-class="article-search" class="el-icon-search" @click.native="toSearch"/>
      </el-input>
      <el-input v-else v-model="keyWord" :class="{ 'search-text': isHideIcon }" size="small" type="text" style="font-size:14px;" placeholder="搜索你有疑问的功能" class="search-input" @focus="hideIcon" @keyup.enter.native="toSearch">
        <svg-icon v-show="!isHideIcon" slot="prefix" icon-class="article-search" class="el-icon-search" />
      </el-input>
    </div>
    <div :class="{'mobile-common-problem':showMobilePage}" class="common-problem">
      <Title v-if="showMobilePage" title="常见问题" class="mb8"/>
      <div v-else class="problem-head">常见问题</div>
      <div v-if="list.length" class="problem-list">
        <div v-for="item in list" :key="item.id" class="problem-item">
          <div class="problem-title ellipsisTwo" @click="toHelpCheck(item)">
            {{ item.title }}
          </div>
          <div v-if="item.intro" class="problem-content">
            <div class="intro">
              答：<span>{{ item.intro }}</span>
              <el-button type="text" size="small" class="to-help-check" @click="toHelpCheck(item)">详细内容 ></el-button>
            </div>
          </div>
        </div>
      </div>
      <empty-placeholder v-else :empty-img="emptyArticle" text="暂未添加文章" image-size="120px" style="margin: 50px 0 24px" />
    </div>
    <div v-if="(hasRouteAuth('crm:help:add') || isAdmin) && showMobilePage" class="mobile-fixed">
      <el-button type="primary" size="small" class="add-article" @click="addArticle">
        添加文章
        <i class="el-icon-plus" style="font-size: 16px;" />
      </el-button>
    </div>
  </div>
</template>
<script>
import { helpList } from '@/api/help';
import helpTitle from '@/assets/help/dialog-title.png';
import mobileHelpTitle from '@/assets/help/mobile-help-title.png';
import dialogTitleMask from '@/assets/help/dialog-title-mask.png';
import emptyArticle from '@/assets/help/empty-article.png';
import EmptyPlaceholder from '@/components/EmptyPlaceholder';
import { getSideBarData } from '@/components/helpCenter/model/sidebarData';
import Title from '@/components/Title';
import getParentName from './mixins/getParentName';
import { hasRouteAuth } from '@/utils/permission';
import { mapState } from 'vuex';

export default {
  components: { EmptyPlaceholder, Title },
  mixins: [getParentName],
  props: {
    routerName: {
      type: String,
      default: ''
    },
    isShowHelp: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      emptyArticle,
      helpTitle,
      mobileHelpTitle,
      dialogTitleMask,
      keyWord: '',
      list: [],
      isHideIcon: false // 获取焦点后隐藏icon且输入框左对齐
    };
  },
  computed: {
    ...mapState({
      'userInfo': state => state.user.userInfo,
      'showMobilePage': state => state.app.showMobilePage
    }),
    sidebarData() {
      const list = getSideBarData();
      return list;
    },
    isAdmin() {
      return this.userInfo.userName === '管理员';
    }
  },
  watch: {
    isShowHelp: {
      handler(val) {
        if (val) {
          this.getList();
        }
      },
      immediate: true
    }
  },
  methods: {
    hasRouteAuth,
    getList() {
      const params = {
        page: 1,
        perPage: 1000,
        model: this.getParentName(this.routerName)
      };
      helpList(params).then(res => {
        const data = res.data;
        this.list = data.items;
      }).catch(err => {
        console.log(err);
      });
    },
    addArticle() {
      this.$emit('closeDialog');
      this.keyWord = '';
      let routeUrl = null;
      routeUrl = this.$router.resolve({
        name: 'helpCreate',
        query: { model: this.routerName }
      });
      if (routeUrl) window.open(routeUrl.href, '_blank');
    },
    hideIcon() {
      this.isHideIcon = true;
    },
    toSearch() {
      let routeUrl = null;
      routeUrl = this.$router.resolve({
        name: 'help',
        query: {
          keyWord: this.keyWord,
          model: this.routerName,
          isSearch: true // 从弹窗搜索进行跳转进入则不走列表页面create中调用接口的逻辑
        }
      });
      if (routeUrl) window.open(routeUrl.href, '_blank');
      this.$emit('closeDialog');
      this.keyWord = '';
    },
    toHelpList() {
      this.$emit('closeDialog');
      this.keyWord = '';
      let routeUrl = null;
      routeUrl = this.$router.resolve({
        name: 'help',
        query: { model: 'newRead' }
      });
      if (routeUrl) window.open(routeUrl.href, '_blank');
    },
    toHelpCheck(item) {
      this.$emit('closeDialog');
      this.keyWord = '';
      let routeUrl = null;
      routeUrl = this.$router.resolve({
        name: 'helpDetail',
        params: { id: item.id },
        query: { model: this.routerName }
      });
      if (routeUrl) window.open(routeUrl.href, '_blank');
    }
  }
};
</script>
<style lang="scss">
.help-dialog {
  .title {
    position: relative;
    width: 100%;
    overflow: hidden;
    height: 115px;
    background: linear-gradient(336deg, #4056FF 0%, #0C3CD0 100%);
    opacity: 1;
    padding-top: 20px;
    box-sizing: content-box;
    .dialog-title-mask {
      position: absolute;
      top: 10px;
      right: 0;
      width: 212px;
      height: 140px;
      img {
        width: 100%;
        height: auto;
      }
    }
    .title-text {
      width: 110px;
      height: 46px;
      margin-left: 10px;
      img {
        width: 100%;
        height: auto;
      }
    }
    .title-btn {
      margin: 0 24px;
      display: flex;
      justify-content: space-between;
      .to-help {
        margin-top: 16px;
        font-size: 18px;
        color: #fff;
        cursor: pointer;
        span {
          text-decoration: underline;
        }
      }
      .add-article {
        width: 88px;
        height: 32px;
        background: #FFFFFF;
        opacity: 1;
        border-radius: 4px;
        z-index: 100;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        line-height: 14px;
        color: #406EFF;
        padding: 0px 0 2px;
        border: none;
      }
    }
  }
  .help-search {
    // position: absolute;
    // top: 135px;
    // left: calc(50% - 200px);
    // width: 400px;
    width: 100%;
    height: 50px;
    background: #FFFFFF;
    box-shadow: 0px 1px 0px #DDE1EA;
    opacity: 1;
    border-radius: 0px;
    .search-input {
      line-height: 50px;
      text-align: center;
      border-radius: 0;
      .el-input__inner {
        border: none;
        padding-left: 130px;
        border-radius: 0;
      }
      .el-input__prefix {
        left: 100px;
        .el-icon-search {
          width: 14px;
          height: 14px;
        }
      }
    }
    .search-text {
      .el-input__inner {
        padding-left: 30px;
      }
    }
  }
  .common-problem {
    margin-top: 44px;
    margin-left: 24px;
    .problem-item {
      margin-bottom: 10px;
    }
    .problem-head {
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 500;
      line-height: 16px;
      color: #333333;
      margin-bottom: 12px;
    }
    .problem-title {
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      line-height: 1.5;
      color: #333333;
      text-decoration: underline;
      cursor: pointer;
      &:hover {
        color: #406eff;
      }
      &::before {
        content: "";
        display: inline-block;
        width: 3px;
        height: 3px;
        background-color: #333;
        border-radius: 50%;
        margin-right: 5px;
        vertical-align: middle;
      }
    }
    .problem-content {
      .intro {
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 400;
        line-height: 20px;
        color: #999999;
        .to-help-check {
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: 400;
          line-height: 14px;
          color: #406EFF;
        }
      }
    }
  }
}
.mobile-help-dialog {
  position: relative;
  height: 100vh;
  padding: 14px 16px 0;
  background: linear-gradient(151deg,#ffffff 18%, #eaedf7 95%);
  .mobile-title {
    height: auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: none;
    padding-top: 0;
    .mobile-title-btn {
      margin: 0;
      .to-help {
        margin-top: 0;
        font-size: 14px;
        font-weight: bold;
        color: #406EFF;
      }
    }
  }
  .help-search {
    box-shadow: none;
  }
  .form-input {
    width: 100%;
    margin: 6px 0 12px;
    & ::v-deep {
      .el-input__prefix {
        left: 10px;
        top: 5px;
      }
    }
    .el-icon-search {
      width: 14px;
      height: 14px;
    }
  }
  .mobile-common-problem {
    margin: 0 0;
    .problem-list {
      margin-left: 6px;
    }
  }
  .mb8 {
    margin-bottom: 8px;
  }
  .mobile-fixed {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: center;
    background-color: #fff;
    padding: 6px 16px;
    .el-button {
      width: 100%;
    }
  }
}
</style>
